package com.haochang.chunkvod.view.vod.tab;

import com.haochang.base.BaseTextView;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

/**
 *@author qintao
 *created at 2016/6/1 15:51
 */

public class TabViewTwo extends RelativeLayout
{

	public TabViewTwo(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		initView(context);
	}

	public TabViewTwo(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initView(context);
	}

	public TabViewTwo(Context context)
	{
		super(context);
		initView(context);
	}

	private BaseTextView tv_tableName;
	private View iv_separator;
	private Context context;
	private int layoutHeight = 50;
	private View view_redLine;
	private int tableNameWidth = 0;

	private void initView(Context context)
	{
		this.context = context;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.view_table_two, this, true);
		android.view.ViewGroup.LayoutParams layout = new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, getTabHeight());
		view.setLayoutParams(layout);
		tv_tableName = (BaseTextView) findViewById(R.id.tv_tableName);
		iv_separator = findViewById(R.id.iv_separator);
		view_redLine = findViewById(R.id.view_redLine);
	}

	public int getTabHeight()
	{
		return DipPxConversion.dipToPx(context, layoutHeight);
	}

	public void setText(String string)
	{
		tv_tableName.setText(string);
		tableNameWidth = (int) tv_tableName.getPaint().measureText(string);
	}

	@Override
	public void setTag(Object tag)
	{
		super.setTag(tag);
		if (tag instanceof Integer)
		{
			if (0 == (Integer) tag)
				this.iv_separator.setVisibility(View.GONE);
			else
				this.iv_separator.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void setSelected(boolean selected)
	{
		tv_tableName.setTextColor(getResources().getColor(selected ? R.color.color_f04e39 : R.color.color_282828));
		view_redLine.setVisibility(selected ? View.VISIBLE : View.GONE);
		LayoutParams para = (LayoutParams) view_redLine.getLayoutParams();
		para.width = tableNameWidth;
		view_redLine.setLayoutParams(para);
		super.setSelected(selected);
	}

	@Override
	public boolean shouldDelayChildPressedState()
	{
		return super.shouldDelayChildPressedState();
	}
}
